package refactorplugin.views;

import java.util.List;

import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Display;

import entity.TaskInfo;

public class TaskTreeRows implements INotificationObject {

	private List<TaskInfo> _rows;
	
	public TaskTreeRows(List<TaskInfo> rows) {
		_rows = rows;
	}

	private TreeComposite getTaskTree(List<TaskInfo> rows) {
		TreeComposite root = new TreeComposite("All Tasks");
		
		for (TaskInfo task : rows) {
			//search for active/performed
			TreeComposite isActiveNode = null;
			String isActiveNodeName = null;
			if (task.isActive())
				isActiveNodeName = "Active tasks";
			else {
				if (task.isLastPerformed())
					isActiveNodeName = "Last performed task";
				else
					isActiveNodeName = "Tasks performed before";
			}
			for (TreeComponent isActiveLevelNode : root.getChildren())
				if (!isActiveLevelNode.isLeaf() && isActiveLevelNode.getName().equals(isActiveNodeName)) {// .getElements().length>0 && isActiveLevelNode.getElements()[0].isActive()==task.isActive()) {
					isActiveNode = (TreeComposite)isActiveLevelNode;
					break;
				}
			if (isActiveNode == null) {
				isActiveNode = new TreeComposite(isActiveNodeName);
				root.addChild(isActiveNode);
			}
			
			TreeComposite module = null;
			String moduleName = task.getTargetModule();
			for (TreeComponent moduleNode : isActiveNode.getChildren())
				if (!moduleNode.isLeaf() && moduleNode.getName().equals(moduleName)) {// .getElements().length>0 && isActiveLevelNode.getElements()[0].isActive()==task.isActive()) {
					module = (TreeComposite)moduleNode;
					break;
				}
			if (module == null) {
				module = new TreeComposite(moduleName);
				isActiveNode.addChild(module);
			}
				
			//search for task declaration
			TreeComposite taskDeclaration = null;
			String taskDeclarationName = task.getTaskName();//.getDescription().substring(0,5);
			for (TreeComponent declarationLevelNode : module.getChildren())
				if (declarationLevelNode.getName().equals(taskDeclarationName)){// .getElements().length>0 && declarationLevelNode.getElements()[0].getDescription().substring(0,5).equals(task.getDescription().substring(0,5))) {
					taskDeclaration = (TreeComposite)declarationLevelNode;
					break;
				}
			if (taskDeclaration == null) {
				taskDeclaration = new TreeComposite(taskDeclarationName);
				module.addChild(taskDeclaration);
			}
			
			//add the element
			taskDeclaration.addChild(new TreeLeaf<TaskInfo>(task.getDescription(),task));
		}
		System.out.println("ROOT "+root.getChildren());
		return root;
	}

	@Override
	public void show(ColumnViewer viewer) {
		System.out.println("TaskTreeRows>Operate");
		TreeViewer treeViewer = (TreeViewer)viewer; 
		if (treeViewer!=null) {
			treeViewer.getTree().clearAll(true);
			System.out.println("TaskTreeRows>clearAll");
			treeViewer.setInput(getTaskTree(_rows));
			treeViewer.refresh();
			treeViewer.expandAll();
		} else
			System.out.println("TaskTreeRows>Viewer = null");
	}

	@Override
	public boolean isTable() {
		return false;
	}

}
